<?php
require_once dirname(__FILE__) . "/../config/functions.php";
$fullUrl = full_url();
include_once dirname(__FILE__) . "/header.php";
?>

<div id="page-wrapper">

    <div class="row">
        <div class="col-lg-12">
            <h1>Members</h1>
            <ol class="breadcrumb">
                <?php
                //count memebers
                $allMemebers = mysql_query("select count(*) as totCount from user where status <> 2");
                $allMemebersCount = mysql_fetch_assoc($allMemebers);
                $allMemebersCount = $allMemebersCount['totCount'];

                $activeMemebers = mysql_query("select count(*) as totCount from user where status = 1");
                $activeMemebersCount = mysql_fetch_assoc($activeMemebers);
                $activeMemebersCount = $activeMemebersCount['totCount'];

                $pendingMemebers = mysql_query("select count(*) as totCount from user where status = 0");
                $pendingMemebersCount = mysql_fetch_assoc($pendingMemebers);
                $pendingMemebersCount = $pendingMemebersCount['totCount'];
                ?>
                <li class="active">
                    <i class="fa fa-dashboard"></i> Filter Members
                    <br/>
                    <b>Total Members:</b> <?php echo $allMemebersCount; ?>&nbsp;&nbsp;&nbsp;<b>Active Members:</b> <?php echo $activeMemebersCount; ?>&nbsp;&nbsp;&nbsp;<b>Pending Members:</b> <?php echo $pendingMemebersCount; ?>
                </li>
            </ol>
            <form class="form-horizontal" role="form" method="get" action="<?php echo strtok($fullUrl, '?'); ?>">
                <div class="form-group">
                    <label for="searchWord" class="col-sm-2 control-label">Search Word</label>
                    <div class="col-sm-5">
                        <input type="text" class="form-control" id="searchWord" name="searchWord" value="<?php
                        if (isset($_GET['searchWord'])) {
                            echo $_GET['searchWord'];
                        }
                        ?>"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="searchBy" class="col-sm-2 control-label">Search By</label>
                    <div class="col-sm-5">
                        <select class="form-control" id="searchBy" name="searchBy">
                            <option value="name" <?php if (isset($_GET['searchBy']) && $_GET['searchBy'] == 'name'): ?>selected=""<?php endif; ?>>Name</option>
                            <option value="email" <?php if (isset($_GET['searchBy']) && $_GET['searchBy'] == 'email'): ?>selected=""<?php endif; ?>>Email</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputOptions" class="col-sm-2 control-label">Options</label>
                    <div class="col-sm-5">
                        <select class="form-control" id="inputOptions" name="status">
                            <option value="">-- Member Status --</option>
                            <option value="true" <?php if (isset($_GET['status']) && $_GET['status'] == 'true'): ?>selected=""<?php endif; ?>>Active</option>
                            <option value="false" <?php if (isset($_GET['status']) && $_GET['status'] == 'false'): ?>selected=""<?php endif; ?>>Pending</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default">Search</button>
                    </div>
                </div>
            </form>

            <?php
            $itemsPerPage = 20;
            $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
            $ofsset = ($page - 1) * $itemsPerPage;
            $limit = "limit $ofsset,$itemsPerPage";

            $query = "SELECT * from user where status <> 2";

            //check for search word
            $searchWord = NULL;
            $searchBy = NULL;
            if (isset($_GET['searchWord']) && $_GET['searchWord']) {
                $searchWord = $_GET['searchWord'];
                $searchBy = $_GET['searchBy'];
                if ($searchBy == "name") {
                    $query .= " and name like '%" . mysql_real_escape_string($searchWord) . "%'";
                } else {
                    $query .= " and email like '%" . mysql_real_escape_string($searchWord) . "%'";
                }
            }


            //check for status filter
            if (isset($_GET['status']) && $_GET['status']) {
                $query .= " and status = " . mysql_real_escape_string($_GET['status']);
            }

            $users = mysql_query("$query order by name asc $limit");
            //count all users
            $usersCount = mysql_query("$query");
            $usersCount = mysql_num_rows($usersCount);

            //calculate the last page number
            $lastPageNumber = (int) ($usersCount / $itemsPerPage);
            if (($usersCount % $itemsPerPage) > 0) {
                $lastPageNumber++;
            }
            ?>
            <table class="table table-bordered">
                <thead><tr><th>#</th><th>Name</th><th>Email</th><th>Username</th><th>Status</th><th>Action</th></tr></thead>
                <tbody>
                    <?php
                    $index = 1;
                    while ($row = mysql_fetch_array($users)) :
                        ?>
                        <tr>
                            <td><?php echo $index; ?></td>
                            <td><?php echo $row['name']; ?></td>
                            <td><?php echo $row['email']; ?></td>
                            <td><?php echo $row['username']; ?></td>
                            <td>
                                <?php
                                if ($row['status'] == 1) {
                                    echo 'Active';
                                } elseif ($row['status'] == 0) {
                                    echo 'Pending';
                                }
                                ?>
                            </td>
                            <td>
                                <a href="member-view.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-eye-open"></span></a>
                                <a href="member-edit.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-pencil"></span></a>
                                <a href="member-delete-confirm.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-remove"></span></a>                                
                            </td>
                        </tr>
                        <?php
                        $index++;
                    endwhile;
                    ?>
                </tbody>
            </table>

            <?php
            if ($lastPageNumber > 1):
                $fullUrl = strtok($fullUrl, '?');
                ?>
                <ul class="pagination">
                    <?php if ($page > 1): ?>
                        <li>
                            <a href="<?php
                            $params = $_GET;
                            $params['page'] = $page - 1;
                            $paramString = http_build_query($params);
                            echo $fullUrl . '?' . $paramString;
                            ?>">«</a>
                        </li>
                    <?php endif; ?>
                    <?php for ($index = 1; $index <= $lastPageNumber; $index++): ?>
                        <?php if ($index > $page - 3 && $index < $page + 3): ?>
                            <li <?php if ($index == $page): ?>class="active"<?php endif; ?>>
                                <a href="<?php
                                $params = $_GET;
                                $params['page'] = $index;
                                $paramString = http_build_query($params);
                                echo $fullUrl . '?' . $paramString;
                                ?>"><?php echo $index; ?></a>
                            </li>
                        <?php endif; ?>
                    <?php endfor; ?>    
                    <?php if ($page < $lastPageNumber): ?>
                        <li>
                            <a href="<?php
                            $params = $_GET;
                            $params['page'] = $page + 1;
                            $paramString = http_build_query($params);
                            echo $fullUrl . '?' . $paramString;
                            ?>">»</a>
                        </li>
                    <?php endif; ?>
                </ul>
            <?php endif; ?>

        </div>
    </div><!-- /.row -->

</div><!-- /#page-wrapper -->

<?php include_once dirname(__FILE__) . "/footer.php"; ?>